//---------------------------------------------------------------------
// File: EntLibDatabaseDeleteStep.cs
// 
// Summary: 
//---------------------------------------------------------------------


using System.Xml;
using BizUnit.BizUnitOM;
using BizUnit.Extensions.EntLib.Utilities;

namespace BizUnit.Extensions.EntLib
{
    /// <summary>
    /// The EntLibDatabaseDeleteStep step uses the supplied connection string, table name and condition to delete rows from the database
    /// </summary>
    /// 
    /// <remarks>
    /// The following shows an example of the Xml representation of this test step.
    /// 
    /// <code escaped="true">
    ///	<TestStep assemblyPath="" typeName="BizUnit.EntLibDatabaseDeleteStep">
    ///		<ConfigFileLocation>..\..\db.config</ConfigFileLocation>
    ///     <DatabaseConfigName>Northwind</DatabaseConfigName>
    ///		<Table>table</Table>
    ///		<Condition>id=1</Condition>
    ///	</TestStep>
    ///	</code>
    ///	
    ///	<list type="table">
    ///		<listheader>
    ///			<term>Tag</term>
    ///			<description>Description</description>
    ///		</listheader>
    ///		<item>
    ///			<term>ConfigFileLocation</term>
    ///			<description>Location of the Database configuration file</description>
    ///		</item>
    ///     <item>
    ///			<term>DatabaseConfigName</term>
    ///			<description>Name of the Database to be used in the configuration file</description>
    ///		</item>
    ///		<item>
    ///			<term>Table</term>
    ///			<description>Table name to delete rows from</description>
    ///		</item>
    ///		<item>
    ///			<term>Condition</term>
    ///			<description>Condition to use</description>
    ///		</item>
    ///	</list>
    ///	</remarks>

    public class EntLibDatabaseDeleteStep : ITestStep, ITestStepOM
    {
        private string configFileLocation;
        private string databaseConfigName;
        private string table;
        private string condition;

        public string ConfigFileLocation
        {
            get
            {
                return configFileLocation;
            }
            set
            {
                configFileLocation = value;
            }
        }
        public string DatabaseConfigName
        {
            get
            {
                return databaseConfigName;
            }
            set
            {
                databaseConfigName = value;
            }
        }
        public string Table
        {
            get
            {
                return table;
            }
            set
            {
                table = value;
            }
        }
        public string Condition
        {
            get
            {
                return condition;
            }
            set
            {
                condition = value;
            }
        }
        /// <summary>
        /// ITestStep.Execute() implementation
        /// </summary>
        /// <param name='testConfig'>The Xml fragment containing the configuration for this test step</param>
        /// <param name='context'>The context for the test, this holds state that is passed beteen tests</param>
        public void Execute(XmlNode testConfig, Context context)
        {
            ConfigFileLocation = context.ReadConfigAsString(testConfig, "ConfigFileLocation");
            DatabaseConfigName = context.ReadConfigAsString(testConfig, "DatabaseConfigName");
            Table = context.ReadConfigAsString(testConfig, "Table");
            Condition = context.ReadConfigAsString(testConfig, "Condition");


        }

        #region ITestStepOM Members

        public void Execute(Context context)
        {
            Validate(context);

            // Build the SQL statement
            string sqlStatement = "delete from " + Table + " where " + Condition;

            context.LogInfo("EntLibDatabaseDeleteStep connecting to database configured in \"{0}\", executing statement \"{1}\"", ConfigFileLocation, sqlStatement);

            // Execute command against specified database
            EntLibDatabaseHelper.ExecuteNonQuery(ConfigFileLocation, DatabaseConfigName, sqlStatement);
        }

        public void Validate(Context context)
        {
            ArgumentValidation.CheckForEmptyString(ConfigFileLocation, "Config File Location");
            ArgumentValidation.CheckForEmptyString(DatabaseConfigName, "Database Config Name");
            ArgumentValidation.CheckForEmptyString(Table, "Table");
            ArgumentValidation.CheckForEmptyString(Condition, "Condition");


        }

        #endregion
    }
}
